package action;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

import java.rmi.RemoteException;

import integration.UserDAORemote;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.*;


import metier.UserBean;

public class Connecter implements Runnable
{
	HttpSession session;
	HttpServletRequest request;
	HttpServletResponse response;

	public void setSession(HttpSession session) {
		this.session = session ;
	}
	
	public HttpSession getSession()
	{
		return this.session;
	}
	
	public void setRequest(HttpServletRequest request)
	{
		this.request = request;
	}
	
	public void setResponse(HttpServletResponse resp)
	{
		this.response = resp;
	}
	
	public void run(){

		UserBean ub = (UserBean)session.getAttribute("ub");
		InitialContext context;
		try {
			context = new InitialContext();
			UserDAORemote uDAO = (UserDAORemote)context.lookup("rmi://localhost:1099/userDAO");//dans renvoi UserDAOImpl
			ub = uDAO.search(ub);
			this.session.setAttribute("ub", ub);
			
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			OutputStream os = response.getOutputStream();
			ObjectOutputStream oos = new ObjectOutputStream(os);
			oos.writeObject(ub);
			oos.flush();
			oos.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}//run()


